import {
  getRuleInfo,
  getRuleInfoByCode,
  saveRuleInfo,
  deleteRuleInfo,
  getRuleItemInfo,
  deleteRuleItemInfo,
  saveRuleItemInfo,
  getEvaluateType,
  getEvaluateBody,
  getGradeInfo,
  getScoringType,
  getScoreRuleCode,
  getRuleItemInfoByCode
} from '@/api'
import { ResultType } from '@/types/index'
import { Effect, Reducer } from 'umi'

export interface IndicatorListType {
  // 编号
  code: string
  // 名称
  name: string
  // 备注说明
  desc: string
  // 创建时间
  createTime: string
}
export interface IndicatorModelState {
  indicatorList: IndicatorListType[]
  indicatorDetail: any[]
  evaluateType: any[]
  evaluateBody: any[]
  gradeList: any[]
  scoreType: any[]
  pointRule: any[]
  itemInfo: any
}
export interface IndicatorModelType {
  namespace: string
  state: IndicatorModelState
  reducers: {
    setState: Reducer<IndicatorModelState>
  }
  effects: {
    function_getRuleInfo: Effect
    function_saveRuleInfo: Effect
    function_deleteRuleInfo: Effect
    function_getRuleItemInfo: Effect
    function_deleteRuleItemInfo: Effect
    function_saveRuleItemInfo: Effect
    function_getEvaluateType: Effect
    function_getEvaluateBody: Effect
    function_getGradeInfo: Effect
    function_getScoringType: Effect
    function_getScoreRuleCode: Effect
    function_getRuleItemInfoByCode: Effect
  }
}
export default <IndicatorModelType>{
  namespace: 'indicatorModel',
  state: {
    indicatorList: [],
    indicatorDetail: [],
    evaluateType: [],
    evaluateBody: [],
    gradeList: [],
    scoreType: [],
    pointRule: [],
    itemInfo: {}
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *function_getRuleInfo({ payload: { name } }, { call, put }) {
      const result: ResultType = yield call(getRuleInfo, name)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            indicatorList: result.obj
          }
        })
      }
    },
    *function_saveRuleInfo({ payload, callback }, { call }) {
      const result: ResultType = yield call(saveRuleInfo, payload)
      callback(result)
    },
    *function_deleteRuleInfo({ payload: { code }, callback }, { call }) {
      const result: ResultType = yield call(deleteRuleInfo, code)
      callback(result)
    },
    *function_getRuleItemInfo({ payload: { ruleCode } }, { call, put }) {
      const result: ResultType = yield call(getRuleItemInfo, ruleCode)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            indicatorDetail: result.obj ?? []
          }
        })
      }
    },
    *function_deleteRuleItemInfo(
      { payload: { criteriaCode, code }, callback },
      { call }
    ) {
      const result: ResultType = yield call(
        deleteRuleItemInfo,
        criteriaCode,
        code
      )
      callback(result)
    },
    *function_saveRuleItemInfo({ payload, callback }, { call }) {
      const result: ResultType = yield call(saveRuleItemInfo, payload)
      callback(result)
    },
    *function_getEvaluateType(_, { call, put }) {
      const result: ResultType = yield call(getEvaluateType)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            evaluateType: result.obj
          }
        })
      }
    },
    *function_getEvaluateBody(_, { call, put }) {
      const result: ResultType = yield call(getEvaluateBody)
      yield put({
        type: 'setState',
        payload: {
          evaluateBody: result
        }
      })
    },
    // 查询学段
    *function_getGradeInfo(_, { call, put }) {
      const result: ResultType = yield call(getGradeInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            gradeList: result.obj
          }
        })
      }
    },
    *function_getScoringType(_, { call, put }) {
      const result: ResultType = yield call(getScoringType)
      if (result.code == 200) {
        yield put({
          type: 'setState',
          payload: {
            scoreType: result.obj
          }
        })
      }
    },
    *function_getScoreRuleCode(_, { call, put }) {
      const result: ResultType = yield call(getScoreRuleCode)
      if (result.code == 200) {
        yield put({
          type: 'setState',
          payload: {
            pointRule: result.obj
          }
        })
      }
    },
    *function_getRuleItemInfoByCode(
      { payload: { ruleCode, code } },
      { call, put }
    ) {
      const result: ResultType = yield call(
        getRuleItemInfoByCode,
        ruleCode,
        code
      )
      if (result.code == 200) {
        yield put({
          type: 'setState',
          payload: {
            itemInfo: result.obj
          }
        })
      }
    }
  }
}
